package org.ssi.web.tags.bean;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.struts2.views.jsp.TagUtils;
import org.ssi.web.struts.bean.Pager;
import org.ssi.web.tags.Globals;

public class TableFillTag extends TagSupport {
	private static final long serialVersionUID = -5604987861884951097L;

	private String name = null;

	private int pagesize = 0;

	private String property = "pager";

	private String scope = null;

	private String height = null;

	private int column = 1;

	private String classStyle = null;

	public String getClassStyle() {
		return classStyle;
	}

	public void setClassStyle(String classStyle) {
		this.classStyle = classStyle;
	}

	public int getColumn() {
		return column;
	}

	public void setColumn(int column) {
		this.column = column;
	}

	public String getHeight() {
		return height;
	}

	public void setHeight(String height) {
		this.height = height;
	}

	public TableFillTag() {
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getProperty() {
		return property;
	}

	public void setProperty(String property) {
		this.property = property;
	}

	public String getScope() {
		return scope;
	}

	public void setScope(String scope) {
		this.scope = scope;
	}

	public int getPagesize() {
		return pagesize;
	}

	public void setPagesize(int pagesize) {
		this.pagesize = pagesize;
	}

	public int doStartTag() throws JspException {
		if (pagesize == 0) {
			Object value = TagUtils.getStack(pageContext).findValue(property);
			if (value == null)
				return SKIP_BODY;

			Pager pager = (Pager) value;
			pagesize = pager.getPagesize();
		}

		int counter = 0;
		if (pageContext.getAttribute(Globals.COUNTER_KEY) != null) {
			counter = ((Integer) pageContext.getAttribute(Globals.COUNTER_KEY)).intValue();
		}
		
		counter++;
		StringBuffer out = new StringBuffer();
		for (; counter <= pagesize; counter++) {
			out.append("<tr");
			if (height != null && height.length() > 0) {
				out.append(" height=\"" + height + "\"");
			}
			if (classStyle != null && classStyle.length() > 0) {
				out.append(" class=\"" + classStyle + "\"");
			}
			out.append(">");
			for (int i = 0; i < column; i++) {
				out.append("<td>&nbsp;</td>");
			}
			out.append("</tr>");
		}
		try
		{

			pageContext.getOut().print(String.valueOf(out));
		}
		catch(IOException e)
		{
			e.printStackTrace();
		}
		pagesize = 0;
		return SKIP_BODY;
	}

	public void release() {
		super.release();
		name = null;
		pagesize = 0;
		property = "pager";
		scope = null;
		height = null;
		column = 1;
		classStyle = null;
	}

}
